home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / smaltalk.lha / smalltalk-1.1.1 / Character.st < prev    next >
Text File  |  1991-09-12  |  5KB  |  232 lines

  1. "======================================================================
  2. |
  3. |   Character Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     16 May 90      Changed from being variableSubclass to
  34. |              variableWordSubclass.
  35. |
  36. | sbyrne     25 Apr 89      created.
  37. |
  38. "
  39.  
  40.  
  41. "*** Having this be variable seems wrong ***"
  42. Magnitude variableWordSubclass: #Character 
  43.       instanceVariableNames: ''
  44.       classVariableNames: ''
  45.       poolDictionaries: ''
  46.       category: nil.
  47.  
  48. Character comment: 
  49. 'My instances represent the 256 characters of the character set.  I provide
  50. messages to translate between integers and character objects, and provide
  51. names for some of the common unprintable characters.' !
  52.  
  53.  
  54. !Character class methodsFor: 'constants'!
  55.  
  56. backspace
  57.     "Returns the character 'backspace'"
  58.     ^Character value: 8
  59. !
  60.  
  61. tab
  62.     "Returns the character 'tab'"
  63.     ^Character value: 9
  64. !
  65.  
  66. nl
  67.     "Returns the character 'nl'"
  68.     ^Character value: 10
  69. !
  70.  
  71. newPage
  72.     "Returns the character 'newPage'"
  73.     ^Character value: 12
  74. !
  75.  
  76. cr
  77.     "Returns the character 'cr'"
  78.     ^Character value: 13
  79. !
  80.  
  81. esc
  82.     "Returns the character 'esc'"
  83.     ^Character value: 27
  84. !
  85.  
  86. space
  87.     "Returns the character 'space'"
  88.     ^Character value: 32    "could also return $ "
  89. !!
  90.  
  91.  
  92. !Character class methodsFor: 'Instance creation'!
  93.  
  94. digitValue: anInteger
  95.     "Returns a character that corresponds to anInteger.  0-9 map to $0-$9,
  96.     10-35 map to $A-$Z"
  97.     (anInteger between: 0 and: 9)
  98.         ifTrue: [ ^Character value: anInteger + 48 ]. "48 == $0"
  99.     (anInteger between: 10 and: 35)
  100.         ifTrue: [ ^Character value: anInteger - 10 + 65 ]. "65 = $A"
  101.     ^self error: 'value not in range 0 to 35'
  102. !!
  103.  
  104.  
  105.  
  106. !Character methodsFor: 'converting'!
  107.  
  108. digitValue
  109.     "Returns the value of self interpreted as a digit.  Here, 'digit' means
  110.     either 0-9, or uppercase A-Z, which maps to 10-35."
  111.     (self between: $0 and: $9) ifTrue: [ ^self asciiValue - 48 ].
  112.                 "48 is $0"
  113.     (self between: $A and: $Z) ifTrue: [ ^self asciiValue - 65 + 10 ].
  114.                 "65 is $A"
  115.     ^self error: 'Invalid digit character'
  116. !!
  117.  
  118.  
  119.  
  120. !Character methodsFor: 'comparing'!
  121.  
  122. < aCharacter
  123.     ^self asciiValue < aCharacter asciiValue
  124. !
  125.  
  126. <= aCharacter
  127.     ^self asciiValue <= aCharacter asciiValue
  128. !
  129.  
  130. > aCharacter
  131.     ^self asciiValue > aCharacter asciiValue
  132. !
  133.  
  134. >= aCharacter
  135.     ^self asciiValue >= aCharacter asciiValue
  136. !!
  137.  
  138.  
  139.  
  140. !Character methodsFor: 'testing'!
  141.  
  142. isDigit
  143.     "True if self is a 0-9 digit"
  144.     ^self between: $0 and: $9
  145. !
  146.  
  147. isLetter
  148.     "True if self is an upper- or lowercase letter"
  149.     ^(self between: $a and: $z) or: [ self between: $A and: $Z ]
  150. !
  151.  
  152. isAlphaNumeric
  153.     "True if self is a letter or a digit"
  154.     ^self isLetter or: [ self isDigit ]
  155. !
  156.  
  157. isLowercase
  158.     "True if self is a lowercase letter"
  159.     ^self between: $a and: $z
  160. !
  161.  
  162. isUppercase
  163.     "True if self is uppercase"
  164.     ^self between: $A and: $Z
  165. !
  166.  
  167. isSeparator
  168.     "Returns true if self is a space, cr, tab, nl, or newPage"
  169.     self > Character space ifTrue: [ ^false ].
  170.     self = Character space ifTrue: [ ^true ].
  171.     self = Character cr ifTrue: [ ^true ].
  172.     self = Character tab ifTrue: [ ^true ].
  173.     self = Character nl ifTrue: [ ^true ].
  174.     self = Character newPage ifTrue: [ ^true ].
  175.     ^false
  176. !
  177.  
  178. isVowel
  179.     "Returns true if self is a, e, i, o, or u; case insensitive"
  180.     | char |
  181.     char _ self asLowercase.
  182.     char = $a ifTrue: [ ^true ].    
  183.     char = $e ifTrue: [ ^true ].    
  184.     char = $i ifTrue: [ ^true ].    
  185.     char = $o ifTrue: [ ^true ].    
  186.     char = $u ifTrue: [ ^true ].    
  187.     ^false
  188. !!
  189.  
  190.  
  191.  
  192. !Character methodsFor: 'Coercion methods'!
  193.  
  194. asLowercase
  195.     "Returns self as a lowercase character if it's an uppercase letter,
  196.      otherwise returns the character unchanged."
  197.     self isUppercase ifFalse: [ ^self ].
  198.     ^Character value: (self asciiValue) + 32
  199. !
  200.  
  201. asUppercase
  202.     "Returns self as a uppercase character if it's an lowercase letter,
  203.      otherwise returns the character unchanged."
  204.     self isLowercase ifFalse: [ ^self ].
  205.     ^Character value: (self asciiValue) - 32
  206. !
  207.  
  208. asSymbol
  209.     "Returns the character self as a symbol."
  210.     ^Symbol internCharacter: self
  211. !!
  212.  
  213.  
  214.  
  215. !Character methodsFor: 'printing'!
  216.  
  217. printOn: aStream
  218.     aStream nextPut: self
  219. !!
  220.  
  221.  
  222.  
  223. !Character methodsFor: 'storing'!
  224.  
  225. storeOn: aStream
  226.     aStream nextPut: $$.
  227.     aStream nextPut: self
  228. !!
  229.  
  230.